﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EasyClassUI.entity;
using EasyClassUI.WebRequest;
using System.Threading.Tasks;

namespace EasyClassUI.forms.blog
{
    public partial class allRequiredBlogsForm : Form
    {
        private StudyGroup group;
        private List<Post> posts;
        private List<Post> AllPosts;
        int groupNum = 0;
        public allRequiredBlogsForm(StudyGroup group)
        {
            InitializeComponent();
            this.group = group;
            groupTitle.Text = group.StudyGroupName;
            initBlogIndexCells(false);
        }
        public allRequiredBlogsForm() {
            InitializeComponent();
            groupTitle.Text = "My Blog";
            initBlogIndexCells(true);
        }

        public void DisplayPosts(Post post) {
            int CommentsCount = 0;
            if (post.Comments == null) {
                CommentsCount = 0;
            }
            else {
                CommentsCount = post.Comments.Count;
            }
            blogIndexCell indexCell = new blogIndexCell(post.Title, "User: " + post.User.UserName, "Description: " + post.Category, post.LikeCount, CommentsCount, post);
            blogIndexDisplayPanel.Controls.Add(indexCell);
        }

        public void initBlogIndexCells(bool isMe)
        {
            // initial all bolgs
            PostService postService = new PostService();
            PostDownloadStatus result = new PostDownloadStatus();
            if (!isMe) {
                result = postService.SearchPostByTitle("", group.StudyGroupID, 100);
            }
            else {
                result = postService.QueryPostsByUser(User.CurrentUser.Token);
            }
            posts = result.Posts.ToList();
            AllPosts = posts;
            List<Post> postToShow = posts.Skip(0).Take(4).ToList();
            Parallel.ForEach(postToShow, p => DisplayPosts(p));
            currentPageNum.Text = groupNum.ToString();
            titalPageNum.Text = Convert.ToInt32((posts.Count / 4)).ToString();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void searchBlogButton_Click(object sender, EventArgs e)
        {
            // search blogs
            blogIndexDisplayPanel.Controls.Clear();
            posts = AllPosts.Where(p => p.Title.ToLower().Contains(searchBlogTextBox.Text.ToLower())).ToList();
            groupNum = 0;
            List<Post> postToShow = posts.Skip(0).Take(4).ToList();
            foreach(Post post in postToShow) {
                DisplayPosts(post);
            }
            currentPageNum.Text = groupNum.ToString();
            titalPageNum.Text = Convert.ToInt32((posts.Count / 4)).ToString();
        }

        private void nextPageButton_Click(object sender, EventArgs e) {
            // next page
            blogIndexDisplayPanel.Controls.Clear();
            groupNum += 1;
            if(groupNum > posts.Count - 1) {
                groupNum = posts.Count;
                return;
            }
            List<Post> postToShow = posts.Skip(groupNum).Take(4).ToList();
            foreach (Post post in postToShow) {
                DisplayPosts(post);
            }
            currentPageNum.Text = groupNum.ToString();
        }

        private void lastPageButton_Click(object sender, EventArgs e) {
            // previous page
            blogIndexDisplayPanel.Controls.Clear();
            groupNum -= 1;
            if (groupNum < 0) {
                groupNum = 0;
                return;
            }
            List<Post> postToShow = posts.Skip(groupNum).Take(4).ToList();
            foreach (Post post in postToShow) {
                DisplayPosts(post);
            }
            currentPageNum.Text = groupNum.ToString();
        }

        private void gotoButton_Click(object sender, EventArgs e) {
            // Go to page XX
            blogIndexDisplayPanel.Controls.Clear();
            if (!int.TryParse(toWhichPageTextBox.Text, out int num)) {
                return;
            }

            if (num > posts.Count - 1 || num < 0) {
                return;
            }
            List<Post> postToShow = posts.Skip(num).Take(4).ToList();
            foreach (Post post in postToShow) {
                DisplayPosts(post);
            }
        }
    }
}
